#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
import wx.calendar
import aux_bd
import wx.lib.mixins.listctrl as listmix
import helper


class Page5(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour((150, 150, 150))
        
        wx.StaticText(self, label="Buscar", pos=(70, 30))

        self.ln = wx.StaticLine(self, -1, pos=(0, 50), style=wx.LI_VERTICAL)
        self.ln.SetSize((800, 3))
        
        #cargar una caja
        self.sb = wx.StaticBox(self, pos=(50, 60))
        self.sb.SetSize((700, 225))

        """
        Esta pagina es para buscar, el listbox inferior devuelve
        los resultados de las consultas
        """

        wx.StaticText(self, label="Clientes", pos=(70, 100))
        wx.StaticText(self, label="Productos", pos=(70, 150))
        wx.StaticText(self, label="Pedidos", pos=(70, 200))

        self.cb1 = wx.CheckBox(self, pos=(200, 100))
        self.cb2 = wx.CheckBox(self, pos=(200, 150))
        self.cb3 = wx.CheckBox(self, pos=(200, 200))

        self.b1 = wx.Button(self, label='Buscar', pos=(350, 145), size=(100,30))
        self.b1.Bind(wx.EVT_BUTTON, self.onClick)

#         self.lc = wx.ListCtrl(self, -1, pos=(50, 350), size=(700, 150), style=wx.LC_REPORT)
        self.list_ctrl = wx.ListCtrl(self, -1, pos=(50, 300),size=(700,200),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         |wx.LC_SORT_ASCENDING
                         )
        
    def onClick(self, event):
        if self.cb1.IsChecked():
            bd = aux_bd.BaseDatos()
            datos = bd.buscar_cliente()
            self.list_ctrl.ClearAll()
            
            self.list_ctrl.InsertColumn(0, 'ID Cliente', wx.LIST_AUTOSIZE)
            self.list_ctrl.InsertColumn(1, 'Nombre', width = 100)
            self.list_ctrl.InsertColumn(2, 'Direccion', width = 240)
            self.list_ctrl.InsertColumn(3, 'Telefono', width = 100)

            
    #         items = musicdata.items()
            index = 0
            for data in datos:
                self.list_ctrl.InsertStringItem(index, str(data[0]))
                self.list_ctrl.SetStringItem(index, 1, data[1])
                self.list_ctrl.SetStringItem(index, 2, data[2])
                self.list_ctrl.SetStringItem(index, 3, str(data[3]))

                
    #             self.list_ctrl.SetItemData(index, key)
                index += 1
     
            # Now that the list exists we can init the other base class,
            # see wx/lib/mixins/listctrl.py
    #         self.itemDataMap = datos
    #         listmix.ColumnSorterMixin.__init__(6)
    #         self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)
     
    #         sizer = wx.BoxSizer(wx.VERTICAL)
    #         sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
    #         self.SetSizer(sizer)
    #         
    #         for j in datos:
    #             print j
    #             for i in j:
    #                 pos = self.lc.InsertStringItem(0, str(i))
    #                 self.lc.SetStringItem(pos, 1, str(i))
    #                 self.lc.SetStringItem(pos, 2, str(i))
    #                 self.lc.SetStringItem(pos, 3, str(i))
    #                 self.lc.SetStringItem(pos, 4, str(i))
#             texto = 'Busqueda satisfactoria'
#             helper.mostrar_msj(texto)
        
        if self.cb2.IsChecked():
            bd = aux_bd.BaseDatos()
            datos = bd.buscar_producto()
            self.list_ctrl.ClearAll()
            
            self.list_ctrl.InsertColumn(0, 'ID Producto', wx.LIST_AUTOSIZE)
            self.list_ctrl.InsertColumn(1, 'Nombre', width = 130)
            self.list_ctrl.InsertColumn(2, 'Precio Publico', width = 100)
            self.list_ctrl.InsertColumn(3, 'Precio Mayor', width = 100)

            
    #         items = musicdata.items()
            index = 0
            for data in datos:
                self.list_ctrl.InsertStringItem(index, str(data[0]))
                self.list_ctrl.SetStringItem(index, 1, data[1])
                self.list_ctrl.SetStringItem(index, 2, str(data[2]))
                self.list_ctrl.SetStringItem(index, 3, str(data[3]))
                
    #             self.list_ctrl.SetItemData(index, key)
                index += 1

#             texto = 'Busqueda satisfactoria'
#             helper.mostrar_msj(texto)
            
        if self.cb3.IsChecked():
            bd = aux_bd.BaseDatos()
            datos = bd.buscar_pedido()
            self.list_ctrl.ClearAll()
            
            self.list_ctrl.InsertColumn(0, 'ID Pedido', width = 74)
            self.list_ctrl.InsertColumn(1, 'Nombre', width = 95)
            self.list_ctrl.InsertColumn(2, 'Pedido', width = 230)
            self.list_ctrl.InsertColumn(3, 'Monto', width = 70)
            self.list_ctrl.InsertColumn(4, 'Fecha', width = 75)
            self.list_ctrl.InsertColumn(5, 'Fecha Entrega', width = 75)
            self.list_ctrl.InsertColumn(6, 'Estado', width = 90)
#             self.list_ctrl.SetColumnWidth(0, 80)
#             self.list_ctrl.SetColumnWidth(1, 100)
#             self.list_ctrl.SetColumnWidth(2, 240)
#             self.list_ctrl.SetColumnWidth(3, 80)
#             self.list_ctrl.SetColumnWidth(4, 90)
#             self.list_ctrl.SetColumnWidth(5, 90)
            
            index = 0
            for data in datos:
                self.list_ctrl.InsertStringItem(index, str(data[0]))
                self.list_ctrl.SetStringItem(index, 1, data[1])
                self.list_ctrl.SetStringItem(index, 2, data[2])
                self.list_ctrl.SetStringItem(index, 3, str(data[3]))
                self.list_ctrl.SetStringItem(index, 4, data[4])
                self.list_ctrl.SetStringItem(index, 5, data[5])
                self.list_ctrl.SetStringItem(index, 6, data[6])

                
                index += 1
     
#             texto = 'Busqueda satisfactoria'
#             helper.mostrar_msj(texto)
        

        self.cb1.SetValue(False)
        self.cb2.SetValue(False)
        self.cb3.SetValue(False)